﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using Aandelendelen.Db;
using Aandelendelen.Obs;

namespace Aandelendelen.Controls
{
	public partial class AdvertiserMyCampaignsControl : AuthenticationRequiredAdvertiserBaseUserControl
	{
		protected override void PreLoadHandler(object sender, EventArgs e)
		{
			base.PreLoadHandler(sender, e);
			this.PopulateCampaigns();
		}

		private void PopulateCampaigns()
		{
			int id = this.UserInfo.Id;

			Query query = new Query("s_adsperuser", "id", id, OleDbType.Integer);

			ResultSet resultSet = (ResultSet)query.Execute();

			for (int i = 0; i < resultSet.Count; i++)
			{
				this.PopulateCampaign(resultSet, i);
			}
		}

		private void PopulateCampaign(ResultSet resultSet, int row)
		{
			this.CreateBannerRow(resultSet, row);

			this.CreateDetailRow(resultSet, row);
		}

		private void CreateBannerRow(ResultSet resultSet, int row)
		{
			string banner = (string)resultSet[row, "BannerUrl"];
			string targetUrl = (string)resultSet[row, "TargetUrl"];
			string toolTip = (string)resultSet[row, "ToolTip"];

			TableRow tableRow = new TableRow();

			TableCell bannerCell = new TableCell();
			bannerCell.CssClass = "myadbanner";
			bannerCell.ColumnSpan = 4;
			HyperLink link = new HyperLink();
			link.ImageUrl = banner;
			link.Text = toolTip;
			link.NavigateUrl = targetUrl;
			link.Target = "_blank";

			bannerCell.Controls.Add(link);

			tableRow.Cells.Add(bannerCell);

			TableCell stateCell = new TableCell();
			stateCell.ColumnSpan = 5;
			bool paidFor = (bool)resultSet[row, "PaidFor"];
			DateTime dt = (DateTime)resultSet[row, "EndDate"];
			bool active = (paidFor == true || dt.Year != 1980);
			stateCell.Text = (active ? "Actief" : "Niet actief");
			stateCell.CssClass = (active ? "myadstateactive" : "myadstateinactive");

			tableRow.Cells.Add(stateCell);

			this.Table1.Rows.Add(tableRow);
		}

		private void CreateDetailRow(ResultSet resultSet, int row)
		{
			this.CreateDetailHeader(resultSet, row);

			this.CreateDetailBody(resultSet, row);

			this.CreateDetailBreak();
		}

		private void CreateDetailHeader(ResultSet resultSet, int row)
		{
			TableRow headerRow = new TableRow();
			headerRow.CssClass = "myadheader";

			this.CreateDetailHeaderCell(headerRow, "Nummer");
			this.CreateDetailHeaderCell(headerRow, "Gestart");
			this.CreateDetailHeaderCell(headerRow, "Beëindigd");
			this.CreateDetailHeaderCell(headerRow, "# Vertoningen");
			this.CreateDetailHeaderCell(headerRow, "Verloopt bij");
			this.CreateDetailHeaderCell(headerRow, "# Kliks");
			this.CreateDetailHeaderCell(headerRow, "Laatste vertoning");
			this.CreateDetailHeaderCell(headerRow, "Laatste klik");
			this.CreateDetailHeaderCell(headerRow, "Kosten");

			this.Table1.Rows.Add(headerRow);
		}

		private void CreateDetailHeaderCell(TableRow headerRow, string text)
		{
			TableCell headerCell = new TableCell();

			headerCell.CssClass = "myadheader";
			headerCell.Text = text;

			headerRow.Cells.Add(headerCell);
		}

		private void CreateDetailBody(ResultSet resultSet, int row)
		{
			TableRow detailRow = new TableRow();
			detailRow.CssClass = "myaddetails";

			this.CreateDetailCell(detailRow, resultSet[row, "Id"].ToString());
			this.CreateDetailCell(detailRow, (DateTime)resultSet[row, "StartDate"]);
			this.CreateDetailCell(detailRow, (DateTime)resultSet[row, "EndDate"]);
			this.CreateDetailCell(detailRow, resultSet[row, "Views"].ToString());
			this.CreateDetailCell(detailRow, resultSet[row, "MaxViews"].ToString());
			this.CreateDetailCell(detailRow, resultSet[row, "Clicks"].ToString());
			this.CreateDetailCell(detailRow, (DateTime)resultSet[row, "LastView"]);
			this.CreateDetailCell(detailRow, (DateTime)resultSet[row, "LastClick"]);
			this.CreateDetailCell(detailRow, ObjectFormatter.GetCurrencyString(resultSet[row, "Cost"]));

			this.Table1.Rows.Add(detailRow);
		}

		private void CreateDetailCell(TableRow tableRow, string text)
		{
			TableCell detailCell = new TableCell();

			detailCell.Text = text;
			detailCell.CssClass = "myaddetails";
			tableRow.Cells.Add(detailCell);
		}

		private void CreateDetailCell(TableRow tableRow, DateTime dt)
		{
			this.CreateDetailCell(tableRow, this.GetDateTimeString(dt));
		}

		private void CreateDetailBreak()
		{
			TableRow row = new TableRow();

			TableCell cell = new TableCell();
			cell.CssClass = "myadbreak";
			cell.ColumnSpan = 9;

			row.Cells.Add(cell);

			this.Table1.Rows.Add(row);
		}

		private string GetDateTimeString(DateTime dt)
		{
			string str = (dt.Year == 1980 ? " -- " : dt.ToString("dd-MM-yyyy, HH:mm:ss"));

			return str;
		}
	}
}